<?php
namespace Admin\Controller;
use \Frame\Libs\BaseController;
use \Admin\Model\UserModel;
use Admin\Model\UserLogModel;

final class UserController extends BaseController
{
	public function index()
	{
        //验证是否登录
        $this->denyAccess();
		$modelObj = UserModel::getInstance();
		$arr = $modelObj->fetchAll();
		$this->smarty->assign("arr",$arr);
		$this->smarty->display("User/index.html");
	}
    public function login()
    {
        //显示登陆页面
        if(IS_GET){
        	$this->smarty->display("User/login.html");
        }else{
        	$data = $_POST;
        	//验证码
        	if(strtolower($data["verify"])==strtolower($_SESSION["captcha"])){
        		$password = md5($data["password"]);
        		//判断账号密码
        		$modelOBJ = UserModel::getInstance();
        		$arr = $modelOBJ->fetchOne("user='$data[username]' and password='$password'");
        		if($arr){
        			//将用户状态信息写入SESSION 静态初始化 开启SESSION
        			$_SESSION["id"] = $arr["id"];
        			$_SESSION["username"] = $arr["username"];
        			//写入日志
        			$this->write_log("登录");
        			$this->ajax_return(["status"=>1,"msg"=>"登录成功","url"=>"?c=index&a=index"]);
        		}else{
        			$this->ajax_return(["status"=>0,"msg"=>"账号密码错误"]);
        		}
        	}else{
        		$this->ajax_return(["status"=>0,"msg"=>"验证码错误"]);
        	}
        }
    }
    //获取验证码
    public function captcha()
    {
        //创建验证码类的对象
        $captchaOBJ = new \Frame\Vendor\Captcha();
    }
    
    public function logout()
    {
    	$this->denyAccess();
        //删除SESSION数据
        unset($_SESSION["usrename"]);
        unset($_SESSION["id"]);
        //删除SESSION文件
        session_destroy();
        //删除SESION对应的COOKIE数据
        setcookie(session_name(),false);
        //跳转到后台登录页面
        $this->jump("用户退出成功","?c=User&a=login");
    }
	public function LockScreen(){
		
	}
}
